<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<form class="layui-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" name="goodsName" id="goodsName" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">供应商</label>
            <div class="layui-input-inline" style="width: 100px;">
                <select id="supplierId" name="supplier.id">
                    <option value="">请选择</option>
                    <c:forEach items="${requestScope.supplierList}" var="s">
                        <option value="${s.id}">${s.name}</option>
                    </c:forEach>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline" style="width: 100px;">
                <select id="status" name="status">
                    <option value="">请选择</option>
                    <option value="1">已付款</option>
                    <option value="0">未支付</option>
                </select>
            </div>
        </div>
        <button lay-submit="" class="layui-btn" id="search-bill-btn" lay-filter="demo1">搜索</button>
    </div>
</form>
<script type="text/html" id="bill-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn" lay-event="add"><i class="layui-icon">&#xe654;</i>添加</button>
        <button class="layui-btn layui-btn-danger" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</button>
    </div>
</script>
<script type="text/html" id="row-tool">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>
<table id="bill-table" lay-filter="bill-filter"></table>
<script type="text/javascript">
    layui.use(['form', 'table', 'layer'], function () {
        var form = layui.form;
        form.render();//后追加到页面的元素，layui不渲染，必须render
        var table = layui.table;
        var layer = layui.layer;
        table.render({
            elem: '#bill-table',
            id: "billTable"
            , toolbar: '#bill-toolbar'
            , url: 'bill/table.html' //数据接口
            , page: true //开启分页
            , limit: 4 //值要在limits列表里
            , limits: [2, 4, 6, 8, 10]
            , cols: [[ //表头
                {type: 'checkbox'},
                {field: 'id', title: 'ID', width: 80}
                , {field: 'goodsName', title: '商品名称'}
                , {
                    field: 'supplier', title: '供应商', templet: function (d) {//d是当前这一行的数据，是一个json对象
                        //当field的值不能直接返回的时候，用templet
                        return d.supplier.name;
                    }
                }
                , {
                    field: 'amount', title: '金额(元)', templet: function (d) {
                        return d.amount / 100.0;
                    }
                }
                , {
                    field: 'status', title: '状态', templet: function (d) {
                        return d.status == 1 ? '<span class="layui-badge layui-bg-orange">已付款</span>' : '<span class="layui-badge layui-bg-green">未付款</span>';
                    }
                }
                , {field: 'createdTime', title: '创建时间'},
                {'title': "操作", toolbar:"#row-tool"}
            ]]
        });
        //监听表格的头部工具栏，toolbar(表格的lay-filter)
        table.on('toolbar(bill-filter)', function (obj) {
            switch (obj.event) {//触发的事件
                case 'add'://对应的是工具栏中按钮的lay-event值
                    openEditLayer(null);
                    break;
                case 'delete':
                    var data = table.checkStatus('billTable').data;//复选框选中的行
                    var ids = new Array();
                    $.each(data, function (i, obj) {
                        ids.push(obj.id);
                    })
                    $.ajax({
                        url: "bill/delete.html",
                        method: "post",
                        data: "ids=" + ids,
                        success: function (json) {
                            if (json.status) {
                                table.reload('billTable', {});
                            } else {
                                layer.msg(json.message)
                            }
                        }
                    })
                    break;
            }
            ;
        })
        //监听行工具事件
        table.on('tool(bill-filter)', function(obj) {
            var data = obj.data;//点击的这一行的数据
            switch(obj.event){
                case 'edit':
                    openEditLayer(data)
                    break;
            }
            //console.log(obj)
        })
        function openEditLayer(data){
            layer.open({
                title: "编辑账单",
                type: 1,
                content: $('#edit-layer').html(),
                area: ['500px', '300px'],
                btn: ['提交', '取消']//可以无限个按钮
                , yes: function (index, layero) {//按钮点击事件，第一个按钮默认叫yes
                    $.ajax({
                        url: "bill/edit.html",
                        method: "post",
                        data: $("#edit-form").serialize(),
                        success: function (json) {
                            if (json.status) {
                                table.reload('billTable', {});
                                layer.close(index)
                            } else {
                                layer.msg(json.message)
                            }
                        }
                    })
                }
                , btn2: function (index, layero) {//第x个按钮 就是btnx
                    layer.close(index)
                },
                success: function (layero, index) {//layer打开成功之后回调
                    form.render();
                    if(data!=null){
                        form.val("edit-form",data);
                        //data.supplier.id 关联对象不能直接赋值
                        //data.status 整数，数字类型的，layui radio赋值要是string类型的
                        form.val("edit-form",{
                            "supplier.id":data.supplier.id,
                            status:data.status+""
                        });
                    }
                }
            })
        }
        //刷新表格的方式1，点击提交按钮
//        $("#search-bill-btn").click(function(){
//            var data={};
//            data.status=$("#status").val();
//            data.goodsName=$("#goodsName").val();
//            data['supplier.id']=$("#supplierId").val();//当参数是关联对象，名字中有.的时候，就这么写
//            table.reload('billTable',{
//                where:data
//            })
//        })
        //刷新表格的方式2，监测表单的提交事件
        //通过 lay-filter='demo1' 的按钮提交的表单，data就是demo所在表单的数据
        form.on('submit(demo1)', function (data) {
            //刷新表格数据
            table.reload('billTable', {
                where: data.field
            })
            return false;//阻止表单提交,页面不跳转
        });
    })
</script>
<script type="text/html" id="edit-layer">
    <form class="layui-form" id="edit-form" lay-filter="edit-form">
        <input type="hidden" name="id"/>
        <div class="layui-form-item">
            <label class="layui-form-label">商品名</label>
            <div class="layui-input-block">
                <input type="text" name="goodsName" required lay-verify="required" placeholder="请输入标题"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">厂商</label>
            <div class="layui-input-inline">
                <select name="supplier.id">
                    <option value="">请选择</option>
                    <c:forEach items="${requestScope.supplierList}" var="s">
                        <option value="${s.id}">${s.name}</option>
                    </c:forEach>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="已付款">
                <input type="radio" name="status" value="0" title="未付款" checked>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">价格</label>
            <div class="layui-input-block">
                <input type="text" name="amount" required lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</script>